package a01_数组;

/**
 * <p>
 * a05_长度最小的子数组复习5
 * </p>
 *
 * @author flyduck
 * @since 2024/12/26
 */
public class a05_长度最小的子数组复习5 {

    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int minValue = Integer.MAX_VALUE;
        int leftIdx = 0;
        for (int rightIdx = 0; rightIdx < nums.length; rightIdx++) {
            sum = sum + nums[rightIdx];

            while (sum >= target) {//满足条件了
                minValue = Math.min(minValue, rightIdx - leftIdx + 1);
                sum = sum - nums[leftIdx];
                leftIdx++;
            }
        }

        if (minValue == Integer.MAX_VALUE) {
            return 0;
        }else {
            return minValue;
        }
    }
}
